package org.pcorp.battlespace.construction.ui.sub.equipement;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.pcorp.battlespace.construction.ui.tools.DialogUi;
import org.pcorp.battlespace.construction.ui.tools.PScrollPane;
import org.pcorp.battlespace.modele.Capacite;
import org.pcorp.battlespace.modele.Equipement;
import org.pcorp.battlespace.utils.CodeCnt;

public class EquipInfoModal extends DialogUi {
	private static final long serialVersionUID = -3126635200775411836L;
	private Equipement equipement;

	public EquipInfoModal(Component parent, Equipement equip) {
		super(JOptionPane.getFrameForComponent(parent), true);
		this.setLayout(new GridBagLayout());
		this.setSize(350, 150);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		equipement = equip;
		this.setTitle(new String("[").concat(equipement.getCode()).concat("] - ").concat(equipement.getNom()));
		this.build();
	}

	private void build() {
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridx = gbc.gridy = 0;
		gbc.gridwidth = gbc.gridheight = 1;
		gbc.weightx = gbc.weighty = 0;

		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.fill = GridBagConstraints.BOTH;

		//JLabel nom = getTextFixe(equipement.getNom().concat(" [").concat(equipement.getCode()).concat("]"), false);
		//JLabel code = getTextFixe(equipement.getCode(), false);
		JLabel masse = getTextFixe(equipement.getMasse());
		masse.setText(masse.getText().concat(" ").concat(CodeCnt.UNITE_MASSE));
		JLabel type = getTextFixe(equipement.getType().concat(" - ").concat(equipement.getSsType()), false);
		//JLabel ssType = getTextFixe(equipement.getSsType(), false);
		JLabel cout = getTextFixe(equipement.getCout());
		JLabel desc = getTextFixe(equipement.getDescription());

		/*JPanel bloc1 = new JPanel();
		bloc1.setLayout(new BoxLayout(bloc1, BoxLayout.X_AXIS));
		bloc1.add(nom);
		bloc1.add(getTextFixe("-", false));
		bloc1.add(code);*/

		//add(nom, gbc);

		/*JPanel bloc2 = new JPanel();
		bloc2.setLayout(new BoxLayout(bloc2, BoxLayout.X_AXIS));
		bloc2.add(type);
		bloc2.add(getTextFixe("-", false));
		bloc2.add(ssType);
		bloc2.setBackground(Color.yellow);*/

		add(type, gbc);
		gbc.gridy += 1;

		//gbc.gridy = 2;
		gbc.weightx = 1;
		gbc.gridwidth = 2;
		add(desc, gbc);
		gbc.gridy += 1;
		gbc.weightx = 1;
		gbc.gridwidth = 1;
		// capacites
		List<Capacite> capacites = equipement.getCapacites();
		JScrollPane capa = new PScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		capa.setPreferredSize(new Dimension(60, 50));
		capa.setBorder(BorderFactory.createEtchedBorder());
		JLabel cap;
		if (capacites!=null) {
			for (Capacite capacite : capacites) {
				cap = getTextFixe(capacite.getNom().concat(" (").concat(Double.toString(capacite.getValeurMax())).concat(")"));
				capa.getViewport().add(cap, -1);
			}
		}
		add(capa, gbc);
		gbc.gridy += 1;
		gbc.weightx = 0;


		/*gbc.anchor = GridBagConstraints.CENTER;
	    gbc.weightx = 1;
		gbc.gridy = 0;
		gbc.gridx = 1;
		JPanel spacer = new JPanel();
		spacer.setBackground(Color.RED);
		add(spacer, gbc);
		gbc.gridy += 1;/*

		/*gbc.gridy = 1;
		spacer = new JPanel();
		spacer.setBackground(Color.RED);
		add(spacer, gbc);
		gbc.gridy += 1;*/

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_END;
		masse.setHorizontalAlignment(SwingConstants.TRAILING);
		add(masse, gbc);
		gbc.gridy += 1; 
		// description
		gbc.gridy += 1;
		// operateur
		gbc.gridy += 1;

		cout.setHorizontalAlignment(SwingConstants.TRAILING);
		add(cout, gbc);
		gbc.gridy += 1;
	}

	public static void openForm(Component parent, Equipement equip) {
		EquipInfoModal form = new EquipInfoModal(parent, equip);
		form.setVisible(true);
	}
}
